package org.suwenqian.airoleplayingchatbackend.controller;

import cn.dev33.satoken.secure.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.suwenqian.airoleplayingchatbackend.common.exception.ServiceException;
import org.suwenqian.airoleplayingchatbackend.common.result.Result;
import org.suwenqian.airoleplayingchatbackend.common.web.Results;
import org.suwenqian.airoleplayingchatbackend.dao.dto.req.LoginReqDTO;
import org.suwenqian.airoleplayingchatbackend.dao.dto.req.RegisterReqDTO;
import org.suwenqian.airoleplayingchatbackend.dao.dto.resp.LoginRespDTO;
import org.suwenqian.airoleplayingchatbackend.dao.entity.UserDO;
import org.suwenqian.airoleplayingchatbackend.dao.mapper.UserMapper;
import org.suwenqian.airoleplayingchatbackend.service.SysLoginService;

@RestController
@RequiredArgsConstructor
@RequestMapping("/ai-role-play/auth")
public class AuthController {

    private final UserMapper userMapper;

    private final SysLoginService sysLoginService;


    /**
     * 用户注册
     * @param requestParm 请求参数
     * @return 空值
     */
    @PostMapping("/register")
    public Result<Void> register(@RequestBody RegisterReqDTO requestParm){
        //1. 检查用户名是否存在
        LambdaQueryWrapper<UserDO> queryWrapper = Wrappers.lambdaQuery(UserDO.class)
                .eq(UserDO::getUsername, requestParm.getUsername());
        if (userMapper.selectCount(queryWrapper) > 0){
            throw new ServiceException("用户名已存在");
        }
        // 2. 对密码进行加密
        String passwordHash = BCrypt.hashpw(requestParm.getPassword(), BCrypt.gensalt());
        UserDO userDO = UserDO.builder()
                .username(requestParm.getUsername())
                .password(passwordHash)
                .email(requestParm.getEmail())
                .status(1)
                .build();
        userMapper.insert(userDO);
        return Results.success();
    }

    /**
     * 用户登录
     * @param requestParm 请求参数
     * @return 返回token
     */
    @PostMapping("/login")
    public Result<LoginRespDTO> login(@RequestBody LoginReqDTO requestParm){
        // 1. 根据用户名查询用户
        UserDO userDO = userMapper.selectOne(Wrappers.lambdaQuery(UserDO.class)
                .eq(UserDO::getUsername, requestParm.getUsername()));
        if (userDO == null){
            throw new ServiceException("用户不存在");
        }
        //2. 校验密码
        if (!BCrypt.checkpw(requestParm.getPassword(), userDO.getPassword())){
            throw new ServiceException("密码错误");
        }
        //3. 生成token
        String token = sysLoginService.login(requestParm);
        return Results.success(LoginRespDTO.builder()
                .token(token)
                .access_token( token)
                .userDOInfo(userDO)
                .build());
    }


}
